﻿package jokers{
	import jokers.*;
	import flash.display.MovieClip;
	import flash.geom.Point;
	
	public class JAlgorithm{
		//创建一个不矛盾的矩阵
		public static function getMatrix(w:int,h:int,num:int = 8):Array{
			var matrix:Array = new Array();

			for (var i:int = 0; i < w; i++){
				matrix[i] = new Array();

				for (var j:int = 0; j < h; j++){
					var tmp_color:int = randomCrystal(num);
					
					if (i>=2 && matrix[i-1][j] == tmp_color && matrix[i-2][j] == tmp_color) {
						while (tmp_color == matrix[i-1][j]) {
							tmp_color = randomCrystal(num);
						}
					}
					
					if (j>=2 && matrix[i][j-1] == tmp_color && matrix[i][j-2] == tmp_color) {
						if (i>=2) {
							while (tmp_color == matrix[i-1][j] || tmp_color == matrix[i][j-1]) {
								tmp_color = randomCrystal(num);
							}
						} else {
							while (tmp_color == matrix[i][j-1]) {
								tmp_color = randomCrystal(num);
							}
						}
					}
					matrix[i][j] = tmp_color;
				}
			}
			return matrix;
		}
		
		public static function randomCrystal(num:int = 8):int{
			var rnd:int = int(Math.random()*num);
			if(rnd == JCrystal.CRYSTAL_IRON){
				rnd = int(Math.random()*num);
			}
			return rnd;
		}
		
		//判断是否需要被消除~
		public static function getDelete(xx:int,yy:int,len:int = 3):Array{
			var jmatrix:JMatrix = JMatrix.getInstance();
			var x:int,y:int;
			var w:int = jmatrix.width;
			var h:int = jmatrix.height;
			
			var num:int;
			var c:MovieClip = jmatrix.getCrystal(xx,yy);
			
			if(c == null) return new Array();
			
			var type:int = JCrystal.getCrystalType(c);
			
			var ret:Array = new Array();
			if(type == JCrystal.CRYSTAL_IRON) return ret;
			
			var jd:JDelete;
			
			//横向判定
			jd = new JDelete();
			jd.addPoint(new Point(xx,yy));
			
			num = 1;
			for(x = xx - 1 ; x >= 0 ; x--){
				c = jmatrix.getCrystal(x,yy);
				if(JCrystal.getCrystalType(c) == type){
					jd.addPoint(new Point(x,yy));
					num++;
				}else{
					break;
				}
			}
			
			for(x = xx + 1 ; x < w ; x++){
				c = jmatrix.getCrystal(x,yy);
				if(JCrystal.getCrystalType(c) == type){
					jd.addPoint(new Point(x,yy));
					num++;
				}else{
					break;
				}
			}
			
			if(len <= num){
				ret.push(jd);
			}
			
			//纵向判定
			jd = new JDelete();
			jd.addPoint(new Point(xx,yy));
			
			num = 1;
			for(y = yy - 1 ; y >= 0 ; y--){
				c = jmatrix.getCrystal(xx,y);
				if(JCrystal.getCrystalType(c) == type){
					jd.addPoint(new Point(xx,y));
					num++;
				}else{
					break;
				}
			}
			
			for(y = yy + 1 ; y < h ; y++){
				c = jmatrix.getCrystal(xx,y);
				if(JCrystal.getCrystalType(c) == type){
					jd.addPoint(new Point(xx,y));
					num++;
				}else{
					break;
				}
			}
			
			if(len <= num){
				ret.push(jd);
			}
			
			return ret;
		}
	
		//获取可被消除点
		public static function getRemovableCrystal(len:int = 3):Point{
			var p:Point = null;
			
			var matrix:Array = JMatrix.getInstance().getMatrix();
			
			//横向交换
			for(var y:int = 0 ; y < JMatrix.getInstance().height ; y++){
				for(var x:int = 0 ; x < JMatrix.getInstance().width - 1 ; x++){
					var mc1:MovieClip = matrix[x][y];
					var mc2:MovieClip = matrix[x + 1][y];
					
					if(mc1 == null || mc2 == null) continue;
					
					if(mc1.sta == JCrystal.STATE_NORMAL && mc2.sta == JCrystal.STATE_NORMAL){
						matrix[x][y] = mc2;
						matrix[x+1][y] = mc1;
						
						var ary1:int = JAlgorithm.getDelete(x,y).length;
						var ary2:int = JAlgorithm.getDelete(x + 1,y).length;
						
						if(ary1 != 0) p = new Point(x+1,y);
						if(ary2 != 0) p = new Point(x,y);
						
						matrix[x][y] = mc1;
						matrix[x+1][y] = mc2;
						
						if(p != null) return p;
					}
				}  
			}
		
			//纵向交换
			for(y = 0 ; y < JMatrix.getInstance().height - 1 ; y++){
				for(x = 0 ; x < JMatrix.getInstance().width ; x++){
					mc1 = matrix[x][y];
					mc2 = matrix[x][y + 1];
					
					if(mc1 == null || mc2 == null) continue;
					
					if(mc1.sta == JCrystal.STATE_NORMAL && mc2.sta == JCrystal.STATE_NORMAL){
						matrix[x][y] = mc2;
						matrix[x][y+1] = mc1;
						
						ary1 = JAlgorithm.getDelete(x,y).length;
						ary2 = JAlgorithm.getDelete(x,y + 1).length;
						
						if(ary1 != 0) p = new Point(x,y+1);
						if(ary2 != 0) p = new Point(x,y);
						
						matrix[x][y] = mc1;
						matrix[x][y+1] = mc2;
						
						if(p != null) return p;
					}
				}  
			}
		
			return null;
		}
	}
}